package com.system.borrow.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.activity.FastDetailActivity;
import com.system.borrow.adapter.FastListAdapter;
import com.system.borrow.bean.FastBean;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.utils.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.Request;
import okhttp3.Response;

/**
 * ================================================
 * 作    者：LuCenly(落尘)
 * 版    本：1.0
 * 创建日期：2016/11/17 23:34
 * 描    述：TODO
 * ================================================
 */
public class FastFragment extends Fragment {
    View myView;
    TextView tv_title;
    ListView lv_fast;
    FastListAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (myView == null) {
            myView = inflater.inflate(R.layout.fragment_fast, container, false);
        } else {
            ViewParent parent = myView.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(myView);
            }
        }
        initView(myView);
        addAction();
        loadData();
        initListener();
        return myView;
    }

    private void initListener() {
        lv_fast.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FastBean fastBean = (FastBean) adapter.getItem(position);
                Intent intent = new Intent(getActivity(), FastDetailActivity.class);
                intent.putExtra("fast",fastBean);
                startActivity(intent);
            }
        });
    }

    private void addAction() {
        adapter = new FastListAdapter(getActivity());
        lv_fast.setAdapter(adapter);
    }

    /**
     * 数据列表
     */
    private void loadData() {
        OkHttpUtils.get(URLDefind.LOAN_LIST)
                .tag(this)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(new DialogCallback<String>(getActivity(),String.class,null) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200){
                                    Gson gson = new Gson();
                                    TypeToken<List<FastBean>> token = new TypeToken<List<FastBean>>(){};
                                    List<FastBean> list = gson.fromJson(json.getJSONArray("data").toString(),token.getType());
                                    adapter.setList(list);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200){
                                    Gson gson = new Gson();
                                    TypeToken<List<FastBean>> token = new TypeToken<List<FastBean>>(){};
                                    List<FastBean> list = gson.fromJson(json.getJSONArray("data").toString(),token.getType());
                                    adapter.setList(list);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }
    private void initView(View myView) {
        tv_title = (TextView) myView.findViewById(R.id.tv_title);
        tv_title.setText("快速贷款");
        lv_fast = (ListView) myView.findViewById(R.id.lv_fast);
    }
}
